package com.websocket;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.context.annotation.Bean;

@Configuration
@EnableWebMvc
@EnableWebSocket
public class WebSocketConfig extends WebMvcConfigurerAdapter implements WebSocketConfigurer {

    public WebSocketConfig() {
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
    	
    	//    	用来注册websocket server实现类，第二个参数是访问websocket的地址
    	SystemWebSocketHandler ss1 = systemWebSocketHandler();
        registry.addHandler(ss1, "/websck.do").addInterceptors(new HandshakeInterceptor());

        System.out.println("registed!");
        SystemWebSocketHandler ss2 = systemWebSocketHandler();
        //        使用Sockjs的注册方法
        registry.addHandler(ss2, "/sockjs/websck/infos").addInterceptors(new HandshakeInterceptor()).withSockJS();
        System.out.println(ss1==ss2);
    }

//    @Bean //SystemWebSocketHandler 单例
    public SystemWebSocketHandler systemWebSocketHandler() {
//        return new InfoSocketEndPoint();
        return new SystemWebSocketHandler();
    }

}
